﻿Imports System.Runtime.CompilerServices
Imports System.Xml
Imports System.IO
Imports System.Text
Imports Sgml

Module XmlDocumentExtention
    Private Const StrXHtmlNsUrl As String = "http://www.w3.org/1999/xhtml"

    ''' <summary>
    ''' XHTMLのNameSpaceManagerを取得します。
    ''' </summary>
    ''' <param name="xmlRes">NameTableを含むXmlDocument</param>
    ''' <param name="prefix">接頭辞文字列</param>
    ''' <returns>XHTMLのNameSpaceManager</returns>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function GetXHtmlNameSpaceManager(ByVal xmlRes As XmlDocument, ByVal prefix As String) As XmlNamespaceManager
        Dim xnm As New XmlNamespaceManager(xmlRes.NameTable)
        xnm.AddNamespace(prefix, StrXHtmlNsUrl)
        Return xnm
    End Function

    ''' <summary>
    ''' 指定したSGML形式の文字列からXMLドキュメントを読み込みます。
    ''' </summary>
    ''' <param name="xmlRes"></param>
    ''' <param name="sgml"></param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub LoadSgml(ByRef xmlRes As XmlDocument, ByVal sgml As String)
        xmlRes = New XmlDocument()
        Using sr As New StreamReader(New MemoryStream(Encoding.UTF8.GetBytes(sgml)))
            Using sgmlr As New SgmlReader()
                sgmlr.InputStream = sr
                xmlRes.Load(sgmlr)
            End Using
        End Using
    End Sub
End Module
